#!/usr/bin/make -f

package = pipelinedb

PREFIX = /usr/lib/$(package)
TOP_BUILDDIR = ../
DESTDIR = $(CURDIR)/debian/$(package)
INSTALL_DIR = $(DESTDIR)$(PREFIX)
BINDIR = $(PREFIX)/bin
LIBDIR = $(PREFIX)/lib

clean:
	dh_clean
	rm -rf $(DESTDIR)
	if [ -e build ] ; then rm -rf build ; fi ;

build:
	touch build

conf:
	(cd $(TOP_BUILDDIR) && ./configure)

pipeline:
	(cd $(TOP_BUILDDIR) && ./configure PIPELINE_VERSION=$(PIPELINE_VERSION) PIPELINE_REVISION=$(PIPELINE_REVISION) --prefix=$(PREFIX) --with-system-tzdata=/usr/share/zoneinfo CFLAGS="$(PIPELINE_CFLAGS)" --disable-rpath $(PIPELINE_WITH_OPTIONS))
	(cd $(TOP_BUILDDIR) && $(MAKE))
	(cd $(TOP_BUILDDIR) && $(MAKE) install)

pipeline_kafka:
	git clone https://github.com/pipelinedb/pipeline_kafka.git
	(cd pipeline_kafka && ./configure && make PG_CONFIG=$(BINDIR)/pg_config install)
	rm -rf pipeline_kafka

pipeline-debug:
	(cd $(TOP_BUILDDIR) && make clean)
	(cd $(TOP_BUILDDIR) && ./configure CFLAGS="-g -O0 -DDUMP_CORE" PIPELINE_VERSION=$(PIPELINE_VERSION) PIPELINE_REVISION=$(PIPELINE_REVISION) --prefix=$(PREFIX) --with-system-tzdata=/usr/share/zoneinfo --enable-cassert --disable-rpath $(PIPELINE_WITH_OPTIONS))
	(cd $(TOP_BUILDDIR) && $(MAKE) PIPELINE_SERVER_BINARY=pipelinedb-debug)
	(cd $(TOP_BUILDDIR)/src/backend && $(MAKE) PIPELINE_SERVER_BINARY=pipelinedb-debug install-server-bin )

clean-libs:
	find $(LIBDIR) -name "*.la" -exec rm {} \;

move-installation:
	mkdir $(DESTDIR)
	cp -r --parents $(PREFIX) $(DESTDIR)

binary-arch: conf pipeline pipeline_kafka pipeline-debug clean-libs move-installation
	dh_installdocs -a
	dh_installchangelogs -a
	dh_strip --exclude=pipelinedb
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_makeshlibs
	dh_shlibdeps -a -l $(LIBDIR)
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-arch

.PHONY: binary binary-arch clean checkroot
